var xmlHttp;

function GetXmlHttpObject(){
    var xmlHttp=null;
    try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }catch (e){
        //Internet Explorer
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if(xmlHttp==null){
        alert("errore, il tuo browser non supporta ajax");
        return;
    }
    return xmlHttp;
}
function aprilogin(){


    xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function stateChanged2(){

        if(xmlHttp.readyState==4 && xmlHttp.status==200){
				
            document.getElementById("pagina").innerHTML=xmlHttp.responseText;
					
        }
    }
    xmlHttp.open("get","/IS/View/GestioneAccessoView/login.html",true);
    xmlHttp.send();



}
function registrazione(){


    xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function stateChanged2(){

        if(xmlHttp.readyState==4 && xmlHttp.status==200){
				
            document.getElementById("pagina").innerHTML=xmlHttp.responseText;
					
        }
    }
    xmlHttp.open("get","/IS/View/GestioneProfiloView/registrazione.html",true);
    xmlHttp.send();



}
function salva(){


    xmlHttp = GetXmlHttpObject();

    var patternCF = /^[a-z]{6}[0-9]{2}[a-z][0-9]{2}[a-z][0-9]{3}[a-z]$/i;
    var patternEmail= /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
    var patternData= /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[-/.](19|20)\d\d/;

    var nome=document.getElementById("nome").value;
    var cognome=document.getElementById("cognome").value;
    var cf=document.getElementById("cf").value;
    var elements = document.getElementsByName('sex');



    var sesso;

    for (i=0;i<elements.length;i++) {
        if(elements[i].checked == true) {   
            sesso=elements[i].value;
            break;
        }
    }


    var data=document.getElementById("data").value;

    var email=document.getElementById("email").value;

    
    if(cf.search(patternCF)==-1){
        alert("codice fiscale errato")
    }
 
    else if (data.search(patternData)==-1){
        alert("data errata")
    }
    else if (email.search(patternEmail)){
        alert("formato email errato")
    }

    else {
        xmlHttp.onreadystatechange=function stateChanged4(){

        if(xmlHttp.readyState==4 && xmlHttp.status==200){
				
            document.getElementById("page").innerHTML=xmlHttp.responseText;
        }
    }
			
    xmlHttp.open("get","/IS/Controller/GestioneProfiloController/registrazione.php?+&nome="+nome+"&cognome="+cognome+"&cf="+cf+"&sesso="+sesso+"&data="+data+"&email="+email,true);
    xmlHttp.send();
    }
}
function login(){
    var patternUser=/^[a-zA-Z]{4}[1-9]*$/;
    var patternPass=/^[a-zA-Z0-9]{1,24}$/;
    var username=document.getElementById("username").value;
    var password=document.getElementById("password").value;
    var table=document.getElementById("table").value;

    if(username=="" ||password==""){
        alert("Inserire le Credenziali");
        return;
    }
    if(username.search(patternUser) ==-1){
        alert("inserire credenziali");
        return;
        
    }
    if(password.search(patternPass) ==-1){
        alert("inserire credenziali");
        return;
        
    }
    
    xmlHttp = GetXmlHttpObject();
    xmlHttp.onreadystatechange=function stateChanged2(){

        if(xmlHttp.readyState==4 && xmlHttp.status==200){
                            
                            
                                
            window.location=xmlHttp.responseText;
                                    
					
					
        }
    }
    xmlHttp.open("get","/IS/Controller/GestioneAccessoController/login.php?username="+username+"&password="+password+"&table="+table,true);
    xmlHttp.send();



}


function esci(){


    window.location.assign("/IS/View/Homepage InfoHelp.html");
    alert("Arrivederci");
}


function recupero(){
    
    xmlHttp = GetXmlHttpObject();


    var email=document.getElementById("email").value;
    var tipo=document.getElementById("table").value;


    var patternEmail=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;
    
    if(email.search(patternEmail)==-1){
        alert("inserire email corretta")
    }
    else{
    xmlHttp.onreadystatechange=function stateChanged4(){

        if(xmlHttp.readyState==4 && xmlHttp.status==200){
				
            alert("Controllare, email inviata");
            window.location="/IS/View/Homepage InfoHelp.html";
        }
    }
			
    xmlHttp.open("get","/IS/Controller/GestioneAccessoController/recupero.php?+&email="+email+"&tipo="+tipo,true);
    xmlHttp.send();
    
    
    }
    
}


function send(){
    
    window.location="/IS/View/GestioneAccessoView/recupero.html";
    
    
}


function resettaLogin(){
    
    document.getElementById("username").value="";
    document.getElementById("password").value="";
    
    
}


function resettaRegistrazione(){
    
    document.getElementById("nome").value="";
    document.getElementById("cognome").value="";
    document.getElementById("cf").value="";
    document.getElementById("data").value="";
    document.getElementById("email").value="";
    
    
    
}